package com.qf.web.controller;

import com.qf.entity.*;
import com.qf.service.CheckItemService;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查项管理
 */
@RestController//直接让方法返回Java对象
@RequestMapping("/checkitem")
public class CheckItemController {

    @Autowired//查找服务
    private CheckItemService checkItemService;

    /**
     * 查询所有检查项
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        System.out.println("查询所有");
        List<CheckItem> checkItemList = checkItemService.findAll();
        if(checkItemList != null && checkItemList.size() > 0){
            Result result = Result.success("请选择检查项");
            result.setData(checkItemList);
            return result;
        }
        return  Result.error("无检查项,请添加检查项");
    }

    /**
     * 新增检查项
     */
    //@RequestBody解析前端得到的json数据,
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            checkItemService.add(checkItem);
        }catch (Exception e){
            //服务新增失败
            e.printStackTrace();
            return Result.error("新增检查项失败");
        }
        return Result.success("新增检查项成功");
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            checkItemService.delete(id);
        }catch (RuntimeException e){
            return Result.error("删除检查项失败");
        }catch (Exception e){
            return Result.error("删除检查项失败");
        }
        return Result.success("删除检查项成功");
    }

    /**
     * 编辑检查项
     * @param checkItem
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
        }catch (Exception e){
            return Result.error("编辑检查项失败");
        }
        return Result.success("编辑检查项成功");
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            CheckItem checkItem = checkItemService.findById(id);
            return  Result.success(checkItem);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return Result.error("查询检查项失败");
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkItemService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }


}
